// Fill out your copyright notice in the Description page of Project Settings.

#include "HelperBlueprintFunctionLibrary.h"

FDateTime UHelperBlueprintFunctionLibrary::UnixTimestampToDateTime(int64 UnixTime)
{
    FDateTime Time = FDateTime::FromUnixTimestamp(UnixTime);
    return Time;
}

int64 UHelperBlueprintFunctionLibrary::DateTimeToUnixTimestamp(FDateTime Time)
{
    int64 Timestamp = Time.ToUnixTimestamp();
    return Timestamp;
}

bool UHelperBlueprintFunctionLibrary::RegexMatch(const FString& Str, const FString& Pattern, TArray<FString>& Result)
{
    FRegexPattern MatherPatter(Pattern);
    FRegexMatcher Matcher(MatherPatter, Str);

    while (Matcher.FindNext())
    {
        Result.Add(Matcher.GetCaptureGroup(0));
    }

    return Result.Num() == 0 ? false : true;
}

void UHelperBlueprintFunctionLibrary::ScanDirectory(TArray<FString>& Files, const FString& FilePath, const FString& Extension)
{
    FString SearchedFiles = FilePath + Extension;

    IFileManager::Get().FindFiles(Files, *SearchedFiles, true, false);

    // GEngine->AddOnScreenDebugMessage(-1, 100, FColor::Red, *(SearchedFiles));

    for (auto& FileName : Files)
    {
        FileName = FilePath + FileName;
        // GEngine->AddOnScreenDebugMessage(-1, 100, FColor::Red, FileName);
    }
}

void UHelperBlueprintFunctionLibrary::GetAvailableKeys(TSet<FString>& Keys)
{
    TArray<FString> Files;
    ScanDirectory(Files, FPaths::ProjectContentDir() + "Materials/", FString("*.uasset"));

    TArray<FString> Result;
    for (auto& FileName : Files)
    {
        Result.Empty();
        if (RegexMatch(FileName, FString("maps[1-9][0-9]"), Result))
        {
            Keys.Add(Result[0]);
        }
    }
}

FString UHelperBlueprintFunctionLibrary::ToTexturePath(const FString& Key)
{
    FString Path = FString::Printf(TEXT("Texture2D'/Game/Materials/%s.%s'"), *(Key), *(Key));
    return Path;
}

FString UHelperBlueprintFunctionLibrary::ToMaterialPath(const FString& Key)
{
    FString Path = FString::Printf(TEXT("Material'/Game/Materials/%s_Mat.%s_Mat'"), *(Key), *(Key));
    return Path;
}

FString UHelperBlueprintFunctionLibrary::PathToKey(const FString& Path)
{
    TArray<FString> Result;
    if (RegexMatch(*(Path), "maps[0-9]{2}", Result) == true)
    {
        // UE_LOG(LogClass, Log, TEXT("match result: %s"), *(Result[0]));
        return *(Result[0]);
    }
    // default to maps01
    return FString("maps01");
}

FString UHelperBlueprintFunctionLibrary::ButtonNameToKey(const FString& BtnName)
{
    TArray<FString> Result;
    if (RegexMatch(*(BtnName), "[0-9]{2}", Result) == true)
    {
        // UE_LOG(LogClass, Log, TEXT("match result: %s"), *(Result[0]));
        return FString::Printf(TEXT("maps%s"), *(Result[0]));
    }
    // default to maps01
    return FString("maps01");
}

UTexture2D* UHelperBlueprintFunctionLibrary::LoadTextureFromPath(const FString& Path)
{
    if (Path.IsEmpty()) return NULL;

    UObject* loadObj = StaticLoadObject(UTexture2D::StaticClass(), NULL, *(Path));
    if (loadObj != nullptr)
    {
        return Cast<UTexture2D>(loadObj);
    }
    else
    {
        UE_LOG(LogClass, Log, TEXT("File %s not found. "), *(Path));
        return NULL;
    }
}

UMaterial* UHelperBlueprintFunctionLibrary::LoadMaterialFromPath(const FString& Path)
{
    if (Path.IsEmpty()) return NULL;

    UMaterial* mt = LoadObject<UMaterial>(NULL, *(Path));
    return mt;
}